package com.antdesignbackend.backend.form.saleDeclarationA.service.impl;


import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.activiti.processlist.entity.ProcessList;
import com.antdesignbackend.backend.activiti.processstay.entity.Processstay;
import com.antdesignbackend.backend.activiti.processstay.service.ProcessstayService;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.util.ActivitiUtil;
import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.entity.Production;
import com.antdesignbackend.backend.service.ProductionService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.form.saleDeclarationA.mapper.SaleDeclarationAMapper;
import com.antdesignbackend.backend.form.saleDeclarationA.entity.SaleDeclarationA;
import com.antdesignbackend.backend.form.saleDeclarationA.service.SaleDeclarationAService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class SaleDeclarationAServiceImpl extends ServiceImpl<SaleDeclarationAMapper, SaleDeclarationA> implements SaleDeclarationAService {

    //表单标识
    private static final Integer Form = Constants.SALEDECLARATIONA;
    @Autowired
    ProcessstayService processstayService;
    @Autowired
    SaleDeclarationAService saleDeclarationAService;
    @Autowired
    ProductionService productionService;
    @Autowired
    AnimalNumberService animalNumberService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void submit(SaleDeclarationA saleDeclarationA) throws RuntimeException {
        if (saleDeclarationAService.save(saleDeclarationA)) {//保存成功
//            approval(saleDeclarationA);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateSubmit(SaleDeclarationA saleDeclarationA) throws RuntimeException {
        if (saleDeclarationAService.updateById(saleDeclarationA)) {//保存成功
//            approval(saleDeclarationA);
        } else {
            throw new RuntimeException("提交失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean updateA(SaleDeclarationA saleDeclarationA) {
        Boolean b = false;
        if (!(b = saleDeclarationAService.updateById(saleDeclarationA))){
            return b;
        }
        SaleDeclarationA saleDeclarationA1=saleDeclarationAService.getById(saleDeclarationA.getId());
        //修改动物明细记录
        UpdateWrapper<AnimalNumber> animalNumberUpdateWrapper=new UpdateWrapper<>();
        AnimalNumber animalNumber=animalNumberService.getById(saleDeclarationA1.getAid());
        if(saleDeclarationA1.getSaleAnimalType().equals("种公畜")){
            animalNumberUpdateWrapper.set("species",animalNumber.getSpecies()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("后备母畜")){
            animalNumberUpdateWrapper.set("reserve",animalNumber.getReserve()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("能繁母畜")){
            animalNumberUpdateWrapper.set("menstruation",animalNumber.getMenstruation()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("哺乳牲畜")){
            animalNumberUpdateWrapper.set("lactation",animalNumber.getLactation()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("保育牲畜")){
            animalNumberUpdateWrapper.set("conservation",animalNumber.getConservation()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("育成牲畜")){
            animalNumberUpdateWrapper.set("commodity",animalNumber.getCommodity()-saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("存栏家禽")){
            animalNumberUpdateWrapper.set("livestock",animalNumber.getLivestock()-saleDeclarationA1.getNumber());
        }
        animalNumberUpdateWrapper.eq("id",saleDeclarationA1.getAid());
        animalNumberService.update(animalNumberUpdateWrapper);
        //新增调出记录
        Production production=new Production();
        production.setEid(saleDeclarationA1.getEid());
        production.setChangeTime(saleDeclarationA1.getSaleDate());
        production.setSituation("调出");
        production.setUid(saleDeclarationA1.getCreateBy());
        production.setAnimalType(saleDeclarationA1.getAnimalType());
        if(saleDeclarationA1.getSaleAnimalType().equals("种公畜")){
            production.setMale(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("后备母畜")){
            production.setFemale(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("能繁母畜")){
            production.setBreedFemale(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("哺乳牲畜")){
            production.setLactation(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("保育牲畜")){
            production.setConservation(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("育成牲畜")){
            production.setGrow(saleDeclarationA1.getNumber());
        }else if(saleDeclarationA1.getSaleAnimalType().equals("存栏家禽")){
            production.setNumber(saleDeclarationA1.getNumber());
        }
        production.setCreateTime(new Date());
        productionService.save(production);
        return b;
    }

    public void approval(SaleDeclarationA saleDeclarationA){
        //根据标识获得流程id
        Integer processId = ActivitiUtil.getProcessIdByForm(Form);
        if (processId == null)
            throw new RuntimeException("该流程不存在！");
        //获得下一个审批节点
        ProcessList processList = ActivitiUtil.getProcessList(processId,0);
        if (processList == null)
            throw new RuntimeException("审批不存在！");
        if (processList.getTypes().equals(Constants.SUCCESSIVELY)){ //依次审批
            //获得下一个审批人
            Reviewer reviewer = ActivitiUtil.getReviewerNext(processList.getId(),0);
            if (reviewer == null)
                throw new RuntimeException("审批人不存在！");
            //创建待审记录
            Processstay processstay = new Processstay();
            processstay.setProcessId(processId);
            processstay.setForm(Form);
            processstay.setFormId(saleDeclarationA.getId());
            processstay.setProcesslistId(processList.getId());
            processstay.setTypes(processList.getTypes());
            processstay.setReviewerId(reviewer.getId());
            processstay.setUserId(reviewer.getReviewer());
            processstay.setLaunchId(saleDeclarationA.getCreateBy());
            processstay.setLaunchTime(saleDeclarationA.getCreateTime());
            processstayService.save(processstay);
        }else { //会签或或签
            //获得下一批审批人
            List<Reviewer> list = ActivitiUtil.getReviewer(processList.getId());
            if (list == null)
                throw new RuntimeException("审批人不存在！");
            for (Reviewer reviewer:list){
                //创建待审记录
                Processstay processstay = new Processstay();
                processstay.setProcessId(processId);
                processstay.setForm(Form);
                processstay.setFormId(saleDeclarationA.getId());
                processstay.setProcesslistId(processList.getId());
                processstay.setTypes(processList.getTypes());
                processstay.setReviewerId(reviewer.getId());
                processstay.setUserId(reviewer.getReviewer());
                processstay.setLaunchId(saleDeclarationA.getCreateBy());
                processstay.setLaunchTime(saleDeclarationA.getCreateTime());
                processstayService.save(processstay);
            }
        }
    }
}
